//represent an integer to a specific base
//usage: convert_base_int 99 7 ==> 101
//4100 16 ==> 1004
//
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;
//get the digit from less significant digit
//1.extract the digit
//2.reverse
string convert_base(unsigned int num, unsigned int base)
{
  string s;
  if(num == 0){
    s = "0";
  }
  else {
    while(num!=0){
      s+=(char)(num%base+'0');
      num/=base;
    }
  }
  //reverse string s
  //  cout<<s<<endl;
  
  for(int i = 0,j = s.length()-1;i<j;i++,j--){
    char temp = s[i];
    s[i] = s[j];
    s[j] = temp;
  }
//  //  cout<<s<<endl;
//   int pos = 0;
//   for(pos = 0;s[pos] == '0';pos++);
//   string ss = s.substr(pos);
//   return ss;
  return s;
}
//from most significant digit
//how can I know the power of the most significant digit at a specific base
string convert_base2(unsigned int num, unsigned int base)
{
  string s;
  //get the position of the most signification base
  int i = 0;//get the position
  for(;num/(int)(pow(base,i))>=base;i++);

  for(int j = i;j>=0;j--){
    s+=(char)(num/(int)(pow(base,j))+'0');
    num = num%(int)(pow(base,j));
  }
  return s;
}


int main(int argc, char *argv[])
{
  int i,base;
  if(argc == 3){
    i = atoi(argv[1]);
    base = atoi(argv[2]);
  }
  else {
    i = 99;
    base = 7;
  }
  cout<<i<<" in base 10  is equal to ";
  cout<<convert_base2(i,base)<<" in base "<<base<<endl;
  return 0;
}
